package org.easymis.crm.modules.crm.controller;

import java.util.List;

import org.easymis.commons.web.BaseController;
import org.easymis.commons.web.utils.Page;
import org.easymis.commons.web.utils.R;
import org.easymis.crm.modules.crm.entitys.mybatis.dto.CrmCustomer;
import org.easymis.crm.modules.crm.service.CrmCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Api(tags = "客户管理")
@Controller
public class CrmCustomerController extends BaseController {
	
	@Autowired
	CrmCustomerService service;
	@ApiOperation(value = "客户管理首页", notes = "JSON ", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = { "/crm/customer/index.html" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String index(@ModelAttribute Page page, ModelMap map) throws Exception {
		return getTplPath(map, "crm/customer/index");
	}

	@ApiOperation(value = "客户分页列表", notes = "html", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = { "/crm/customer/list.html" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String list(@ModelAttribute Page page, ModelMap map) throws Exception {
		return getTplPath(map, "crm/customer/list");
	}

	@ApiOperation(value = "客户分页列表", notes = "JSON ", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@RequestMapping(value = { "/crm/customer/list.json" }, method = { RequestMethod.GET, RequestMethod.POST })
	public R listJson(@ModelAttribute Page page, ModelMap map) throws Exception {
		PageHelper.startPage(page.getCurPage(), page.getPageSize());
		List roleList = service.getList(new CrmCustomer());
		PageInfo<CrmCustomer> p = new PageInfo<CrmCustomer>(roleList);
		return new R().put("pageInfo", p);
	}

	@ApiOperation(value = "添加客户", notes = "html", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = { "/crm/customer/add.html" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String forwardAdd(@ModelAttribute Page page, ModelMap map) throws Exception {
		
		return getTplPath(map, "crm/customer/add");
	}

	@ApiOperation(value = "添加客户", notes = "JSON ", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@RequestMapping(value = { "/crm/customer/save.do" }, method = { RequestMethod.GET, RequestMethod.POST })
	public R save(@ModelAttribute Page page, ModelMap map,CrmCustomer bean) throws Exception {
		service.save(bean);
		return new R().ok();
	}

	@ApiOperation(value = "删除客户", notes = "JSON ", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@RequestMapping(value = { "/crm/customer/delete.do" }, method = { RequestMethod.GET, RequestMethod.POST })
	public R delete(@ModelAttribute Page page, ModelMap map,String customerId) throws Exception {
		service.delete(customerId);
		return new R().ok();
	}

	@ApiOperation(value = "编辑客户", notes = "html", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = { "/crm/customer/edit.html" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String forwardEdit(@ModelAttribute Page page, ModelMap map) throws Exception {
		return getTplPath(map, "edit");
	}

	@ApiOperation(value = "修改客户", notes = "JSON ", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@RequestMapping(value = { "/crm/customer/update.do" }, method = { RequestMethod.GET, RequestMethod.POST })
	public R update(@ModelAttribute Page page, ModelMap map,CrmCustomer vo) throws Exception {
		CrmCustomer bean=service.findById(vo.getCustomerId());
		service.update(bean);
		return new R().ok();
	}

	@ApiOperation(value = "客户详情", notes = "JSON ", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@RequestMapping(value = { "/crm/customer/view.json" }, method = { RequestMethod.GET, RequestMethod.POST })
	public R viewJson(@ModelAttribute Page page, ModelMap map,String customerId) throws Exception {
		return new R().put("customer", service.findById(customerId));
	}

	@ApiOperation(value = "客户详情", notes = "html", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = { "/crm/customer/view.html" }, method = { RequestMethod.GET, RequestMethod.POST })
	public String view(@ModelAttribute Page page, ModelMap map) throws Exception {
		return getTplPath(map, "crm/customer/view");
	}

}
